import numpy as np
import matplotlib.pyplot as plt
from scipy import integrate, inf
from matplotlib.patches import ConnectionPatch
x = np.linspace(0.01, 5, 15)


def to_be_integrate(t, _x):     # 被积函数
    return t * np.exp(-(t**2)) * np.exp(-2*t*_x)


e2 = np.exp(-(x*x))
sqrtPi = np.sqrt(np.pi)

A = np.exp(-(x**2)) / ((np.pi**0.5) * x)
b = (1+2*x)

B1 = [(1 - 2 * integrate.quad(to_be_integrate, 0, inf, args=(i, ))[0]) for i in x]
B2 = (e2 / (sqrtPi*x))
erfc = B1*B2

f3 = e2/(x*sqrtPi)

C1 = (2-3*np.exp(-(1+2*x))-2*x*np.exp(-(1+2*x))) / ((1+2*x)**2)
propose = e2*(1-C1)/(sqrtPi*x)

f9 = np.exp(-(x*x))

D1 = (e2/50) + ((np.exp(-(x*x/2))) / (2*(x+1)))
f10 = 2*np.sqrt(2)*D1

# E1 = 2*(x*x+2)*np.sqrt(-(x*x/2))
f11 = 2*(x*x+2)*np.exp(-(x*x/2)) / (sqrtPi*x*(x*x+3))
print(f11)

plt.figure(figsize=(16, 5), dpi=98)
p1 = plt.subplot(121, aspect=5 / 2.5)
p2 = plt.subplot(122, aspect=0.5 / 0.05)

line_width = 2
p1.semilogy(x, erfc, '-0', lw=line_width, label='erfc (x)')
p1.semilogy(x, propose, '-o', lw=line_width, label='the proposed upper bound')
p1.semilogy(x, f3, '-v', lw=line_width, label='the upper bound in (3)')
p1.semilogy(x, f9, '-3', lw=line_width, label='the upper bound in (9)')
p1.semilogy(x, f10, '-s', lw=line_width, label='the upper bound in (10)')
p1.semilogy(x, f11, '-4', lw=line_width, label='the upper bound in (11)')

p2.semilogy(x, erfc, '-0', lw=line_width, label='erfc (x)')
p2.semilogy(x, propose, '-o', lw=line_width, label='the proposed upper bound')
p2.semilogy(x, f3, '-v', lw=line_width, label='the upper bound in (3)')
p2.semilogy(x, f9, '-3', lw=line_width, label='the upper bound in (9)')
p2.semilogy(x, f10, '-s', lw=line_width, label='the upper bound in (10)')
p2.semilogy(x, f11, '-4', lw=line_width, label='the upper bound in (11)')

p1.axis([0.0, 5.01, 1 / (10**10), 1])
p1.grid(True)
p1.legend()

tx0 = 3.2
tx1 = 3.25
ty0 = 8 / (10**6)
ty1 = 4 / (10**6)

p2.axis([tx0, tx1, ty1, ty0])
p2.grid(True)
p2.legend()

sx = [tx0, tx1, tx1, tx0, tx0]
sy = [ty0, ty0, ty1, ty1, ty0]
p1.plot(sx, sy, "purple")

xy = (tx1, ty1)
xy2 = (tx0, ty1)
con = ConnectionPatch(xyA=xy2, xyB=xy, coordsA="data", coordsB="data",
                      axesA=p2, axesB=p1)
p2.add_artist(con)

xy = (tx1, ty0)
xy2 = (tx0, ty0)
con = ConnectionPatch(xyA=xy2, xyB=xy, coordsA="data", coordsB="data",
                      axesA=p2, axesB=p1)
p2.add_artist(con)


# plt.ylim(np.log10(1/(10**10)), 0)
# plt.legend()

# plt.savefig('compare.eps', format='eps')
# plt.savefig('compare.eps', dpi=60, format='eps')

plt.show()
# out_fig = plt.gcf()
# out_fig.savefig('compare.eps', dpi=600, format='eps')